// Date: 2021-05-03
// This Stata do-file replicates tables and figures in
// ”Making a Market: Infrastructure, Integration, and the Rise of Innovation”, 
// by David Andersson, Thor Berger and Erik Prawitz.
//
// Stata version MP 15.1
clear all

// Set main directory 
 
// ----------------------- Style ----------------------- // 


global coefplot_options vert msymbol(O) mcolor(black) lcolor(black) ///
	ciopts(lcolor(black) mcolor(black) recast(rcap) msize(large)) ///
	plotregion(lwidth(none)) scheme(s1mono) 

global coefplot_options2 vert msymbol(O) scheme(s1mono) ///
	ciopts(lcolor(black) mcolor(black) recast(rcap) msize(large)) ///
	plotregion(lwidth(none))

global estoptions replace style(tex) numbers label collabels(none)  ///
	cells(b(star fmt(3)) se(par fmt(3))) ///
	starlevels($^{*}$ 0.1 $^{**}$ 0.05 $^{***}$ 0.01) posthead(\midrule)  ///
	prefoot(\addlinespace[0.1cm]) postfoot(\bottomrule)

	



// ----------------------- Controls etc ----------------------- // 

global leastc_ctrls lareaX1* elev_meanX1* elev_stdX1* cost_meanX* ///
	slope_meanX1* ldistnearestnodeX1* 
global ctrls  lpop1865X1* anypatpre60X1*  llonX1* llatX1*  ldisttownX1* ///
	firmspercap1865X1* mfgpopshare1865X1* 


// Least-cost variables
global leastcost  ldist_leastcostX1870 ldist_leastcostX1880 ///
	ldist_leastcostX1890 ldist_leastcostX1900
global leastcost_all  ldist_leastcostX*

// Region unit for FE
global region region8

// Clustering unit
global cl_unit m_id


// Baseline Sample
global smpl endpoint==0 & urban==0



// --------------------------------------------------------------------- //
// MAIN DATA
// --------------------------------------------------------------------- //
use "data/main_MaM", clear

// --------------------------------------------------------------------- //
//
//	Tables
//
// --------------------------------------------------------------------- //


// TABLE 1: First-stage estimates
eststo clear 
eststo: xi: xtreg d5km_rail $leastcost i.year if $smpl, fe cl($cl_unit)
estadd ysumm
eststo: xi: xtreg d5km_rail $leastcost i.year $leastc_ctrls if $smpl, fe cl($cl_unit)
estadd ysumm
eststo: xi: xtreg d5km_rail $leastcost i.year $ctrls $leastc_ctrls  if $smpl, fe cl($cl_unit)
estadd ysumm
eststo: xi: xtreg d5km_rail $leastcost $region##i.year $ctrls $leastc_ctrls  if $smpl, fe cl($cl_unit)
estadd ysumm
estout using "output/table1.tex", keep($leastcost) mlabels(none) ///
	indicate("Local Geography$\times\$Year FE=*slope_meanX1*" ///
	"Pre-Rail Controls$\times\$Year FE = *lpop*" ///
	"Region FE$\times\$Year FE= *region8*" ) ///
	stats(N ymean, fmt(0 3) labels("Observations" "Mean dep. var.")) $estoptions ///
	prehead(Dependent variable: &  \multicolumn{4}{c}{Network Connection (=1)} ///
	\\ \cmidrule(lr){2-5}  \\)

// TABLE 2: The Effect of Network Connections on Local Innovative Activity
eststo clear 
eststo: xi: xtreg anypatents d5km_rail i.$region*i.year $leastc_ctrls $ctrls if $smpl, fe cl($cl_unit)
estadd ysumm
eststo: xi: xtreg patentspc d5km_rail i.$region*i.year $leastc_ctrls $ctrls if $smpl, fe cl($cl_unit)
estadd ysumm
eststo: xi: xtreg numpoispc d5km_rail i.$region*i.year $leastc_ctrls $ctrls if $smpl, fe cl($cl_unit)
estadd ysumm
eststo: xi: xtreg numpersonspc d5km_rail i.$region*i.year $leastc_ctrls $ctrls if $smpl, fe cl($cl_unit)
estadd ysumm
eststo: xi: xtreg numfirmspc d5km_rail i.$region*i.year $leastc_ctrls $ctrls if $smpl, fe cl($cl_unit)
estadd ysumm
eststo: xi: xtreg patperpoi d5km_rail i.$region*i.year $leastc_ctrls $ctrls if $smpl, fe cl($cl_unit)
estadd ysumm
eststo: xi: xtreg patperperson d5km_rail i.$region*i.year $leastc_ctrls $ctrls if $smpl, fe cl($cl_unit)
estadd ysumm
eststo: xi: xtreg patperfirm d5km_rail i.$region*i.year $leastc_ctrls $ctrls if $smpl, fe cl($cl_unit)
estadd ysumm
estout using "output/table2A.tex", keep(d5km_rail)  mlabels(none) $estoptions ///
	prehead(Dependent variable: & Any patent & Patents per capita & \multicolumn{3}{c}{Inventors per capita} & \multicolumn{3}{c}{Patents per inventor}\\ ///
	\cmidrule(lr){2-2} \cmidrule(lr){3-3} \cmidrule(lr){4-6}  \cmidrule(lr){7-9}  ///
	& & & All & Independent & Firm & All & Independent & Firm  \\  ///
	\cmidrule(lr){4-4} \cmidrule(lr){5-5} \cmidrule(lr){6-6} \cmidrule(lr){7-7} \cmidrule(lr){8-8}  \cmidrule(lr){9-9}  & ///
	\\ \textit{Panel A: OLS} )
eststo clear 
eststo, add(fstat e(widstat)): xi: xtivreg2 anypatents (d5km_rail = $leastcost) i.$region*i.year $ctrls $leastc_ctrls if $smpl, fe cl($cl_unit) 
estadd ysumm
eststo, add(fstat e(widstat)): xi: xtivreg2 patentspc (d5km_rail = $leastcost) i.$region*i.year $ctrls $leastc_ctrls if $smpl, fe cl($cl_unit) 
estadd ysumm
eststo, add(fstat e(widstat)): xi: xtivreg2 numpoispc (d5km_rail = $leastcost) i.$region*i.year $ctrls $leastc_ctrls if $smpl, fe cl($cl_unit)
estadd ysumm
eststo, add(fstat e(widstat)): xi: xtivreg2 numpersonspc (d5km_rail = $leastcost) i.$region*i.year $ctrls $leastc_ctrls if $smpl, fe cl($cl_unit)
estadd ysumm
eststo, add(fstat e(widstat)): xi: xtivreg2 numfirmspc (d5km_rail = $leastcost) i.$region*i.year $ctrls $leastc_ctrls if $smpl, fe cl($cl_unit) 
estadd ysumm
eststo, add(fstat e(widstat)): xi: xtivreg2 patperpoi (d5km_rail = $leastcost) i.$region*i.year $ctrls $leastc_ctrls if $smpl, fe cl($cl_unit) 
estadd ysumm
eststo, add(fstat e(widstat)): xi: xtivreg2 patperperson (d5km_rail = $leastcost) i.$region*i.year $ctrls $leastc_ctrls if $smpl, fe cl($cl_unit) 
estadd ysumm
eststo, add(fstat e(widstat)): xi: xtivreg2 patperfirm (d5km_rail = $leastcost) i.$region*i.year $ctrls $leastc_ctrls if $smpl, fe cl($cl_unit) 
estadd ysumm
estout using "output/table2B.tex", keep(d5km_rail)  mlabels(none) $estoptions ///
	stats(fstat N ymean, fmt(2 0 3) labels("First-Stage F-stat"  "Observations" "Mean dep. var.")) ///
	indicate("Municipality FE = _Iyear_1840" "Region FE$\times\$Year FE= *regXyea*"  "Local Geography$\times\$Year FE=*slope_meanX1*" "Pre-Rail Controls$\times\$Year FE = *lpop*" ) ///
	prehead(\\ \textit{Panel B: 2SLS}) 

	
// TABLE 3: The Effect of Network Connections on the Local Technical Profile
eststo clear 
eststo: xi: xtreg patents_newclasspc d5km_rail i.$region*i.year $leastc_ctrls $ctrls if $smpl, fe cl($cl_unit)
estadd ysumm
eststo: xi: xtreg patents_oldclasspc d5km_rail i.$region*i.year $leastc_ctrls $ctrls if $smpl , fe cl($cl_unit)
estadd ysumm
eststo: xi: xtreg patents_newindustrypc d5km_rail i.$region*i.year $leastc_ctrls $ctrls if $smpl, fe cl($cl_unit)
estadd ysumm
eststo: xi: xtreg patents_oldindustrypc d5km_rail i.$region*i.year $leastc_ctrls $ctrls if $smpl , fe cl($cl_unit)
estadd ysumm
eststo: xi: xtreg patents_toppc d5km_rail i.$region*i.year $leastc_ctrls $ctrls if $smpl , fe cl($cl_unit)
estadd ysumm
eststo: xi: xtreg patents_nontoppc d5km_rail i.$region*i.year $leastc_ctrls $ctrls if $smpl, fe cl($cl_unit)
estadd ysumm
eststo: xi: xtreg patents_top1pc d5km_rail i.$region*i.year $leastc_ctrls $ctrls if $smpl, fe cl($cl_unit)
estadd ysumm
eststo: xi: xtreg patents_nontop1pc d5km_rail i.$region*i.year $leastc_ctrls $ctrls if $smpl , fe cl($cl_unit)
estadd ysumm
eststo: xi: xtreg jaffe d5km_rail i.$region*i.year $leastc_ctrls $ctrls if $smpl, fe cl($cl_unit)
estadd ysumm
eststo: xi: xtreg jaffe_14 d5km_rail i.$region*i.year $leastc_ctrls $ctrls if $smpl, fe cl($cl_unit)
estadd ysumm
estout using "output/table3A.tex", keep(d5km_rail)  mlabels(none) $estoptions ///
	prehead(Dependent variable:  &  \multicolumn{8}{c}{Patents per capita} &  \multicolumn{2}{c}{Tech. proximity} ///
	\\  \cmidrule(lr){2-9} \cmidrule(lr){10-11} ///
	& \multicolumn{4}{c}{Technology class} & \multicolumn{4}{c}{Local economic structure} & Fields & Sectors ///
	\\ \cmidrule(lr){2-5} \cmidrule(lr){6-9}  \cmidrule(lr){10-10}  \cmidrule(lr){11-11} ///
	& Novel & Non-novel & Novel & Non-novel & Existing  & Non-existing  & Leading  & Non-leading &  &  \\ ///
	& field & field & sector & sector & sector & sector & sector & sector & &  \\ ///
	\cmidrule(lr){2-2}	\cmidrule(lr){3-3} \cmidrule(lr){4-4} \cmidrule(lr){5-5} \cmidrule(lr){6-6} \cmidrule(lr){7-7} ///
	\cmidrule(lr){8-8} \cmidrule(lr){9-9}  & ///
	\\ \textit{Panel A: OLS} )
eststo clear 
eststo, add(fstat e(widstat)): xi: xtivreg2 patents_newclasspc (d5km_rail = $leastcost) i.$region*i.year $ctrls $leastc_ctrls if $smpl, fe cl($cl_unit)
estadd ysumm
eststo, add(fstat e(widstat)): xi: xtivreg2 patents_oldclasspc (d5km_rail = $leastcost) i.$region*i.year $ctrls $leastc_ctrls if $smpl, fe cl($cl_unit)
estadd ysumm
eststo, add(fstat e(widstat)): xi: xtivreg2 patents_newindustrypc (d5km_rail = $leastcost) i.$region*i.year $ctrls $leastc_ctrls if $smpl, fe cl($cl_unit)
estadd ysumm
eststo, add(fstat e(widstat)): xi: xtivreg2 patents_oldindustrypc (d5km_rail = $leastcost) i.$region*i.year $ctrls $leastc_ctrls if $smpl, fe cl($cl_unit)
estadd ysumm
eststo, add(fstat e(widstat)): xi: xtivreg2 patents_toppc (d5km_rail = $leastcost) i.$region*i.year $ctrls $leastc_ctrls if $smpl, fe cl($cl_unit)
estadd ysumm
eststo, add(fstat e(widstat)): xi: xtivreg2 patents_nontoppc (d5km_rail = $leastcost) i.$region*i.year $ctrls $leastc_ctrls if $smpl, fe cl($cl_unit)
estadd ysumm
eststo, add(fstat e(widstat)): xi: xtivreg2 patents_top1pc (d5km_rail = $leastcost) i.$region*i.year $ctrls $leastc_ctrls if $smpl, fe cl($cl_unit)
estadd ysumm
eststo, add(fstat e(widstat)): xi: xtivreg2 patents_nontop1pc (d5km_rail = $leastcost) i.$region*i.year $ctrls $leastc_ctrls if $smpl, fe cl($cl_unit)
estadd ysumm
eststo, add(fstat e(widstat)): xi: xtivreg2 jaffe (d5km_rail = $leastcost) i.$region*i.year $ctrls $leastc_ctrls if $smpl, fe cl($cl_unit)
estadd ysumm
eststo, add(fstat e(widstat)): xi: xtivreg2 jaffe_14 (d5km_rail = $leastcost) i.$region*i.year $ctrls $leastc_ctrls if $smpl, fe cl($cl_unit)
estadd ysumm
estout using "output/table3B.tex", keep(d5km_rail)  mlabels(none) $estoptions ///
	stats(fstat N ymean, fmt(2 0 3) labels("First-Stage F-stat"  "Observations" "Mean dep. var.")) ///
	indicate("Municipality FE = _Iyear_1840" "Region FE$\times\$Year FE= *regXyea*"  "Local Geography$\times\$Year FE=*slope_meanX1*" "Pre-Rail Controls$\times\$Year FE = *lpop*" ) ///
	prehead(\\ \textit{Panel B: 2SLS}) 

// TABLE 4: The Effect of Network Connections on patent transfers along the network
eststo clear 
eststo: xi: xtreg anytransfers d5km_rail i.$region*i.year $leastc_ctrls $ctrls if $smpl , fe cl($cl_unit)
estadd ysumm
eststo: xi: xtreg anyrail_transfer5km d5km_rail i.$region*i.year $leastc_ctrls $ctrls if $smpl, fe cl($cl_unit)
estadd ysumm
eststo: xi: xtreg anynonrail_transfer5km d5km_rail i.$region*i.year $leastc_ctrls $ctrls if $smpl, fe cl($cl_unit)
estadd ysumm
eststo: xi: xtreg anymuni_transfer d5km_rail i.$region*i.year $leastc_ctrls $ctrls if $smpl, fe cl($cl_unit)
estadd ysumm
eststo: xi: xtreg transferspc d5km_rail i.$region*i.year $leastc_ctrls $ctrls if $smpl , fe cl($cl_unit)
estadd ysumm
eststo: xi: xtreg rail_transfer5kmpc d5km_rail i.$region*i.year $leastc_ctrls $ctrls if $smpl, fe cl($cl_unit)
estadd ysumm
eststo: xi: xtreg nonrail_transfer5kmpc d5km_rail i.$region*i.year $leastc_ctrls $ctrls if $smpl, fe cl($cl_unit)
estadd ysumm
eststo: xi: xtreg muni_transferpc d5km_rail i.$region*i.year $leastc_ctrls $ctrls if $smpl, fe cl($cl_unit)
estadd ysumm
estout using "output/table4A.tex",  keep(d5km_rail)  mlabels(none) $estoptions ///
	prehead(Dependent variable: &  \multicolumn{4}{c}{Any transfer} & \multicolumn{4}{c}{Transfers per capita} \\ ///
	\cmidrule(lr){2-5} \cmidrule(lr){6-9} ///
	& All &  Connected &  Non-connected &  In-municip. &  All &  Connected &  Non-connected &  In-municip.  \\ ///
	\cmidrule(lr){2-2} \cmidrule(lr){3-3} \cmidrule(lr){4-4} \cmidrule(lr){5-5} \cmidrule(lr){6-6} \cmidrule(lr){7-7} \cmidrule(lr){8-8} \cmidrule(lr){9-9} & ///
	\\ \textit{Panel A: OLS} )
eststo clear 
eststo, add(fstat e(widstat)): xi: xtivreg2 anytransfers (d5km_rail = $leastcost) i.$region*i.year $ctrls $leastc_ctrls if $smpl, fe cl($cl_unit)
estadd ysumm
eststo, add(fstat e(widstat)): xi: xtivreg2 anyrail_transfer5km  (d5km_rail = $leastcost) i.$region*i.year $ctrls $leastc_ctrls if $smpl , fe cl($cl_unit)
estadd ysumm
eststo, add(fstat e(widstat)): xi: xtivreg2 anynonrail_transfer5km  (d5km_rail = $leastcost) i.$region*i.year $ctrls $leastc_ctrls if $smpl, fe cl($cl_unit)
estadd ysumm
eststo, add(fstat e(widstat)): xi: xtivreg2 anymuni_transfer  (d5km_rail = $leastcost) i.$region*i.year $ctrls $leastc_ctrls if $smpl , fe cl($cl_unit)
estadd ysumm
eststo, add(fstat e(widstat)): xi: xtivreg2 transferspc (d5km_rail = $leastcost) i.$region*i.year $ctrls $leastc_ctrls if $smpl, fe cl($cl_unit)
estadd ysumm
eststo, add(fstat e(widstat)): xi: xtivreg2 rail_transfer5kmpc  (d5km_rail = $leastcost) i.$region*i.year $ctrls $leastc_ctrls if $smpl , fe cl($cl_unit)
estadd ysumm
eststo, add(fstat e(widstat)): xi: xtivreg2 nonrail_transfer5kmpc  (d5km_rail = $leastcost) i.$region*i.year $ctrls $leastc_ctrls if $smpl, fe cl($cl_unit)
estadd ysumm
eststo, add(fstat e(widstat)): xi: xtivreg2 muni_transferpc  (d5km_rail = $leastcost) i.$region*i.year $ctrls $leastc_ctrls if $smpl , fe cl($cl_unit)
estadd ysumm
estout using "output/table4B.tex",  keep(d5km_rail)  mlabels(none) $estoptions ///
	stats(fstat N ymean, fmt(2 0 3) labels("First-Stage F-stat"  "Observations" "Mean dep. var.")) ///
	indicate("Municipality FE = _Iyear_1840" "Region FE$\times\$Year FE= *regXyea*"  "Local Geography$\times\$Year FE=*slope_meanX1*" "Pre-Rail Controls$\times\$Year FE = *lpop*" ) ///
	prehead(\\ \textit{Panel B: 2SLS})
	

// TABLE 5: The Effect of Network Connections on transfers by distance to the local economic structure
eststo clear 
eststo: xi: xtreg transfers_newclasspc d5km_rail i.$region*i.year $leastc_ctrls $ctrls if $smpl, fe cl($cl_unit)
estadd ysumm
eststo: xi: xtreg transfers_oldclasspc d5km_rail i.$region*i.year $leastc_ctrls $ctrls if $smpl , fe cl($cl_unit)
estadd ysumm
eststo: xi: xtreg transfers_newindustrypc d5km_rail i.$region*i.year $leastc_ctrls $ctrls if $smpl, fe cl($cl_unit)
estadd ysumm
eststo: xi: xtreg transfers_oldindustrypc d5km_rail i.$region*i.year $leastc_ctrls $ctrls if $smpl , fe cl($cl_unit)
estadd ysumm
eststo: xi: xtreg transfers_toppc d5km_rail i.$region*i.year $leastc_ctrls $ctrls if $smpl , fe cl($cl_unit)
estadd ysumm
eststo: xi: xtreg transfers_nontoppc d5km_rail i.$region*i.year $leastc_ctrls $ctrls if $smpl, fe cl($cl_unit)
estadd ysumm
eststo: xi: xtreg transfers_top1pc d5km_rail i.$region*i.year $leastc_ctrls $ctrls if $smpl, fe cl($cl_unit)
estadd ysumm
eststo: xi: xtreg transfers_nontop1pc d5km_rail i.$region*i.year $leastc_ctrls $ctrls if $smpl , fe cl($cl_unit)
estadd ysumm
estout using "output/table5A.tex",  keep(d5km_rail)  mlabels(none) $estoptions ///
	prehead(Dependent variable: &  \multicolumn{8}{c}{Transfers per capita} \\ \cmidrule(lr){2-9}  ///
	& \multicolumn{4}{c}{Technology class} & \multicolumn{4}{c}{Local economic structure} \\ ///
	\cmidrule(lr){2-5} \cmidrule(lr){6-9} ///
	& Novel & Non-novel & Novel & Non-novel & Existing  & Non-existing  & Leading  & Non-leading  \\ ///
	& field & field & sector & sector & sector & sector & sector & sector  \\ ///	
	\cmidrule(lr){2-2} \cmidrule(lr){3-3} \cmidrule(lr){4-4} \cmidrule(lr){5-5} ///
	\cmidrule(lr){6-6} \cmidrule(lr){7-7} \cmidrule(lr){8-8} \cmidrule(lr){9-9} & ///
	\\ \textit{Panel A: OLS} )
	
eststo clear 
eststo, add(fstat e(widstat)): xi: xtivreg2 transfers_newclasspc (d5km_rail = $leastcost) i.$region*i.year $ctrls $leastc_ctrls if $smpl, fe cl($cl_unit)
estadd ysumm
eststo, add(fstat e(widstat)): xi: xtivreg2 transfers_oldclasspc (d5km_rail = $leastcost) i.$region*i.year $ctrls $leastc_ctrls if $smpl, fe cl($cl_unit)
estadd ysumm
eststo, add(fstat e(widstat)): xi: xtivreg2 transfers_newindustrypc (d5km_rail = $leastcost) i.$region*i.year $ctrls $leastc_ctrls if $smpl, fe cl($cl_unit)
estadd ysumm
eststo, add(fstat e(widstat)): xi: xtivreg2 transfers_oldindustrypc (d5km_rail = $leastcost) i.$region*i.year $ctrls $leastc_ctrls if $smpl, fe cl($cl_unit)
estadd ysumm
eststo, add(fstat e(widstat)): xi: xtivreg2 transfers_toppc (d5km_rail = $leastcost) i.$region*i.year $ctrls $leastc_ctrls if $smpl, fe cl($cl_unit)
estadd ysumm
eststo, add(fstat e(widstat)): xi: xtivreg2 transfers_nontoppc  (d5km_rail = $leastcost) i.$region*i.year $ctrls $leastc_ctrls if $smpl , fe cl($cl_unit)
estadd ysumm
eststo, add(fstat e(widstat)): xi: xtivreg2 transfers_top1pc  (d5km_rail = $leastcost) i.$region*i.year $ctrls $leastc_ctrls if $smpl, fe cl($cl_unit)
estadd ysumm
eststo, add(fstat e(widstat)): xi: xtivreg2 transfers_nontop1pc (d5km_rail = $leastcost) i.$region*i.year $ctrls $leastc_ctrls if $smpl, fe cl($cl_unit)
estadd ysumm
estout using "output/table5B.tex",  keep(d5km_rail)  mlabels(none) $estoptions ///
	stats(fstat N ymean, fmt(2 0 3) labels("First-Stage F-stat"  "Observations" "Mean dep. var.")) ///
	indicate("Municipality FE = _Iyear_1840" "Region FE$\times\$Year FE= *regXyea*"  "Local Geography$\times\$Year FE=*slope_meanX1*" "Pre-Rail Controls$\times\$Year FE = *lpop*" ) ///
	prehead(\\ \textit{Panel B: 2SLS}) 
	
	
// TABLE 6: The Effect of Network Connections on patent transfers by legal category
eststo clear 
eststo: xi: xtreg transfers_indp2firmpc d5km_rail i.$region*i.year $leastc_ctrls $ctrls if $smpl , fe cl($cl_unit)
estadd ysumm
eststo: xi: xtreg transfers_indp2indppc d5km_rail i.$region*i.year $leastc_ctrls $ctrls if $smpl , fe cl($cl_unit)
estadd ysumm
eststo: xi: xtreg transfers_firm2firmpc d5km_rail i.$region*i.year $leastc_ctrls $ctrls if $smpl , fe cl($cl_unit)
estadd ysumm
eststo: xi: xtreg transfers_firm2indppc d5km_rail i.$region*i.year $leastc_ctrls $ctrls if $smpl , fe cl($cl_unit)
estadd ysumm
eststo: xi: xtreg transfers_NOTindp2firmpc d5km_rail i.$region*i.year $leastc_ctrls $ctrls if $smpl , fe cl($cl_unit)
estadd ysumm
estout using "output/table6A.tex",  keep(d5km_rail)  mlabels(none) $estoptions ///
	prehead(Dependent variable: & \multicolumn{5}{c}{Transfers per capita} \\ \cmidrule(lr){2-6} ///
	& Independent (I) & \multicolumn{4}{c}{Other combinations} \\ \cmidrule(lr){3-6}  ///
	& -- Firm (F) & I--I  & F--F & F--I & All \\ ///
	\cmidrule(lr){2-2}  \cmidrule(lr){3-3} \cmidrule(lr){4-4} \cmidrule(lr){5-5}  \cmidrule(lr){6-6} ///
	\\ \textit{Panel A: OLS} )
eststo clear 
eststo, add(fstat e(widstat)): xi: xtivreg2 transfers_indp2firmpc (d5km_rail = $leastcost) i.$region*i.year $ctrls $leastc_ctrls if $smpl , fe cl($cl_unit)
estadd ysumm
eststo, add(fstat e(widstat)): xi: xtivreg2 transfers_indp2indppc  (d5km_rail = $leastcost) i.$region*i.year $ctrls $leastc_ctrls if $smpl , fe cl($cl_unit)
estadd ysumm
eststo, add(fstat e(widstat)): xi: xtivreg2 transfers_firm2firmpc  (d5km_rail = $leastcost) i.$region*i.year $ctrls $leastc_ctrls if $smpl , fe cl($cl_unit)
estadd ysumm
eststo, add(fstat e(widstat)): xi: xtivreg2 transfers_firm2indppc  (d5km_rail = $leastcost) i.$region*i.year $ctrls $leastc_ctrls if $smpl , fe cl($cl_unit)
estadd ysumm
eststo, add(fstat e(widstat)): xi: xtivreg2 transfers_NOTindp2firmpc  (d5km_rail = $leastcost) i.$region*i.year $ctrls $leastc_ctrls if $smpl , fe cl($cl_unit)
estadd ysumm
estout using "output/table6B.tex",  keep(d5km_rail)  mlabels(none) $estoptions ///
	stats(fstat N ymean, fmt(2 0 3) labels("First-Stage F-stat"  "Observations" "Mean dep. var.")) ///
	indicate("Municipality FE = _Iyear_1840" "Region FE$\times\$Year FE= *regXyea*"  "Local Geography$\times\$Year FE=*slope_meanX1*" "Pre-Rail Controls$\times\$Year FE = *lpop*" ) ///
	prehead(\\ \textit{Panel B: 2SLS}) 
	
	
// --------------------------------------------------------------------- //
//
//	Figures
//
// --------------------------------------------------------------------- //

// FIGURE 1: The rise of innovation in Sweden

use "data\aggregate1", clear
twoway (tsline patents, lpattern(solid) lcolor(black) legend(label(1 "Patents (PRV)"))) ///
	(tsline us_patents, lpattern(dash) lcolor(black) legend(label(2 "Patents (USPTO)"))) ///
	(tsline indppatent, lcolor(cranberry) ytitle(Number of patents)  legend(col(2) label(3 "Independent inventors (PRV)"))) ///
	(tsline firmpatent ,lcolor(navy) legend(label(4 "Firms (PRV)"))    ),  ///
	xlabel(1850 1860 1870 1880 1890 1900 1910, angle(45) labgap(2)) scheme(s1mono) plotregion(lwidth(none))
graph export "output/figure1A.pdf", replace

use "data\aggregate2", clear
twoway (tsline transfer_firm2firm, lcolor(cranberry) legend(label(1 "Firm-Firm"))) ///
	(tsline transfer_firm2indp, lcolor(cranberry) lpattern(longdash) legend(label(2 "Firm-Independent"))) ///
	(tsline transfer_indp2firm, lcolor(navy) legend(label(3 "Independent-Firm"))) ///
	(tsline transfer_indp2indp, lcolor(navy) lpattern(dash) legend(label(4 "Independent-Independent"))), ///
	ytitle("No. of patent transfers") xtitle("Year") xlabel(1860 "<1860" 1870 1880 1890 1900, angle(45) labgap(2)) ///
	scheme(s1mono) plotregion(lwidth(none))  
graph export "output/figure1B.pdf", replace

use "data\aggregate3", clear
twoway (line herfindahl_pop year if year>=1860 & year<=1910,  yaxis(2) lcolor(cranberry)) ///
	(line herfindahl_numpois year if year>=1860 & year<=1910, lcolor(eltgreen)) ///
	(line herfindahl_pat year if year>=1860 & year<=1910, lcolor(navy)) , ///
	ytitle("Population", axis(2)) ytitle("Inventors and Patents") ///
	legend(col(3)) xtitle("") xlabel(1860 1870 1880 1890 1900, angle(45) labgap(2))scheme(s1mono) plotregion(lwidth(none))
graph export "output/figure1C.pdf", replace

use "data\aggregate4", clear
twoway (line d5km_rail year, yaxis(1) lcolor(navy) lstyle(connected)) ///
	(line passengersm year, yaxis(2) lcolor(cranberry) lpattern(dash)) , ///
	ytitle("Share municipalities connected") ytitle("Passengers (millions)", axis(2)) ///
	leg(lab(1 "Network connections") lab(2 "Passengers"))  ///
	xtitle("Year") yl(0(0.2)1) yl(0(10)50, axis(2)) ///
	xlabel(1860(10)1900, angle(45) labgap(2)) scheme(s1mono) plotregion(lwidth(none))
graph export "output/figure1D.pdf", replace



	
	
	
// FIGURE 3: Rollout of the railroad network and the spread of innovative activity
use "data/main_MaM", clear
eststo clear 
xi: xtivreg2 anypatents ( d5km_rail1900X* = $leastcost_all) i.$region*i.year $ctrls $leastc_ctrls  if $smpl, fe cl($cl_unit)
estimates store any_1
xi: xtreg anypatents d5km_rail1900X* i.$region*i.year $ctrls $leastc_ctrls  if $smpl, fe cl($cl_unit)
estimates store any_2
xi: xtivreg2 patentspc ( d5km_rail1900X* = $leastcost_all) i.$region*i.year $ctrls $leastc_ctrls  if $smpl, fe cl($cl_unit)
estimates store pat_1
xi: xtreg patentspc d5km_rail1900X* i.$region*i.year $ctrls $leastc_ctrls  if $smpl, fe cl($cl_unit)
estimates store pat_2
xi: xtivreg2 numpoispc ( d5km_rail1900X* = $leastcost_all) i.$region*i.year $ctrls $leastc_ctrls  if $smpl, fe cl($cl_unit)
estimates store poi_1
xi: xtreg numpoispc d5km_rail1900X* i.$region*i.year $ctrls $leastc_ctrls  if $smpl, fe cl($cl_unit)
estimates store poi_2
xi: xtivreg2 patperpoi ( d5km_rail1900X* = $leastcost_all) i.$region*i.year $ctrls $leastc_ctrls  if $smpl, fe cl($cl_unit)
estimates store patpoi_1
xi: xtreg patperpoi d5km_rail1900X* i.$region*i.year $ctrls $leastc_ctrls  if $smpl, fe cl($cl_unit)
estimates store patpoi_2
xi: xtivreg2 jaffe ( d5km_rail1900X* = $leastcost_all) i.$region*i.year $ctrls $leastc_ctrls  if $smpl, fe cl($cl_unit)
estimates store jaffe_1
xi: xtreg jaffe d5km_rail1900X* i.$region*i.year $ctrls $leastc_ctrls  if $smpl, fe cl($cl_unit)
estimates store jaffe_2
xi: xtivreg2 transferspc ( d5km_rail1900X* = $leastcost_all) i.$region*i.year $ctrls $leastc_ctrls  if $smpl, fe cl($cl_unit)
estimates store transf_1
xi: xtreg transferspc d5km_rail1900X* i.$region*i.year $ctrls $leastc_ctrls  if $smpl, fe cl($cl_unit)
estimates store transf_2

coefplot (any_1,  mcolor(cranberry))(any_2, mcolor(navy) msymbol(t)), ///
	legend(off) $coefplot_options2 keep(d5km_rail1900*) ///
	ytitle(Any patents) xtitle(Years) xline(1870, lstyle(grid)) ///
	ylabel(, nogrid) yline(0, lstyle(grid)) xlabel(, angle(45)) 
graph export "output/figure3A.pdf", replace

coefplot (pat_1,  mcolor(cranberry))(pat_2, mcolor(navy) msymbol(t)), ///
	legend(off) $coefplot_options2 keep(d5km_rail1900*) ///
	ytitle(Patents per capita) xtitle(Years) xline(1870, lstyle(grid)) ///
	ylabel(, nogrid) yline(0, lstyle(grid)) xlabel(, angle(45)) 
graph export "output/figure3B.pdf", replace

coefplot (poi_1, mcolor(cranberry))(poi_2, mcolor(navy) msymbol(t)), ///
	legend(off) $coefplot_options2 keep(d5km_rail1900*) ///
	ytitle(Inventors per capita) xtitle(Years) xline(1870, lstyle(grid)) ///
	ylabel(, nogrid) yline(0, lstyle(grid)) xlabel(, angle(45)) 
graph export "output/figure3C.pdf", replace

coefplot (patpoi_1, mcolor(cranberry))(patpoi_2, mcolor(navy) msymbol(t)), ///
	legend(off)	$coefplot_options2 keep(d5km_rail1900*) ///
	ytitle(Patents per inventor) xtitle(Years) xline(1870, lstyle(grid)) ///
	ylabel(, nogrid) yline(0, lstyle(grid)) xlabel(, angle(45)) 
graph export "output/figure3D.pdf", replace

coefplot (jaffe_1, mcolor(cranberry))(jaffe_2, mcolor(navy) msymbol(t)), ///
	legend(off) $coefplot_options2 keep(d5km_rail1900*) ///
	ytitle(Technological proximity) xtitle(Years) xline(1870, lstyle(grid)) ///
	ylabel(, nogrid) yline(0, lstyle(grid)) xlabel(, angle(45)) 
graph export "output/figure3E.pdf", replace

coefplot (transf_1, mcolor(cranberry))(transf_2, mcolor(navy) msymbol(t)), ///
	legend(off) $coefplot_options2 keep(d5km_rail1900*) ///
	ytitle(Transfers per capita) xtitle(Years) xline(1870, lstyle(grid)) ///
	ylabel(, nogrid) yline(0, lstyle(grid)) xlabel(, angle(45))
graph export "output/figure3F.pdf", replace



